<template>
    <base-modal
        :title="initData.pageType === 'edit' ? ' 编辑' : ' 添加'"
        cancelText="取消"
        okText="保存"
        :width="900"
        v-on="$listeners"
        @ok="handleOk"
        :confirmLoading="loading.isSaving"
    >
        <a-form-model
            ref="form"
            :model="formInfo"
            :rules="rules"
            :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
            :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
        >
            <a-row :gutter="24">
                <a-col v-if="initData.typeId == 12" :md="12" :sm="12">
                    <a-form-model-item label="序号" prop="code">
                        <BaseInput placeholder="请输入" v-model.trim="formInfo.code" />
                    </a-form-model-item>
                </a-col>
                <a-col :md="12" :sm="12">
                    <a-form-model-item label="名称" prop="name">
                        <BaseInput placeholder="请输入" v-model.trim="formInfo.name" />
                    </a-form-model-item>
                </a-col>
            </a-row>
        </a-form-model>
    </base-modal>
</template>

<script>
import {
    addSelectListByType,
    updateSelectListByType,
    getSelectListByTypeDetail,
    addSelectListTreeByType,
    updateSelectListTreeByType,
    getSelectListTreeByTypeDetail,
} from '@/api/businessAffairs/common.js'
import BaseModal from '@/components/BaseModal'
export default {
    name:'a'+Date.now(),
    components: {
        BaseModal,
    },
    props: {
        initData: {
            type: Object,
            default() {
                return {}
            },
        },
    },
    data() {
        return {
            loading: {
                isSaving: false,
            },
            formInfo: {
                name: '',
            },
            rules: {
                name: [{ required: true, message: '请输入名称', trigger: 'change' }],
                code: [{ required: true, message: '请输入序号', trigger: 'change' }],
            },
        }
    },
    created() {
        this.getDetail()
    },
    methods: {
        validateForm() {
            let flag = false
            this.$refs.form.validate((valid) => {
                flag = valid
            })
            return flag
        },
        async getDetail() {
            const { id } = this.initData
            if (!id) return
            console.log(this.initData)
            if (this.initData.pageType == 'addchild') return
            const params = {
                id,
            }
            if (this.initData.typeId == 12) {
                const [err, res] = await getSelectListTreeByTypeDetail(params)
                if (!err) {
                    this.formInfo = {
                        ...this.formInfo,
                        ...res.data,
                    }
                }
            } else {
                const [err, res] = await getSelectListByTypeDetail(params)
                if (!err) {
                    this.formInfo = {
                        ...this.formInfo,
                        ...res.data,
                    }
                }
            }
        },
        async handleOk() {
            if (!this.validateForm()) return
            if (this.loading.isSaving) return
            this.loading.isSaving = true
            const { id, name, code } = this.formInfo
            let params = {}
            if (this.initData.typeId == 12) {
                if (this.initData.pageType == 'add' || this.initData.pageType == 'addchild') {
                    params = {
                        id,
                        name,
                        code,
                        type: this.initData.typeId,
                        parentId: this.initData.id || void 0,
                    }
                } else {
                    params = {
                        id,
                        name,
                        code,
                        type: this.initData.typeId,
                        parentId: this.initData.parentId || void 0,
                    }
                }
            } else {
                params = {
                    id,
                    name,
                    code,
                    type: this.initData.typeId,
                    parentId: this.initData.id || void 0,
                }
            }

            console.log(params)
            const { pageType } = this.initData
            if (this.initData.typeId == 12) {
                const fn = pageType === 'edit' ? updateSelectListTreeByType : addSelectListTreeByType
                const [err, res] = await fn(params)
                if (!err) {
                    this.$message.success('成功')
                    this.$emit('confirm', this.initData.typeId)
                }
            } else {
                const fn = pageType === 'edit' ? updateSelectListByType : addSelectListByType
                const [err, res] = await fn(params)
                if (!err) {
                    this.$message.success('成功')
                    this.$emit('confirm', this.initData.typeId)
                }
            }

            this.loading.isSaving = false
        },
    },
}
</script>

<style lang="less" scoped>
form .ant-mentions,
form textarea.ant-input {
    height: auto !important;
    width: 100%;
}
</style>